package com.ctgu.utils;

import com.ctgu.model.entity.SystemLog;
import com.ctgu.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component //把类交给spring管理
@EnableScheduling//开启定时任务
public class Task {

    @Autowired
    private SystemLogService systemLogService;

    //添加定时任务
    //该cron表达式的意思为每天的20:40，执行下方的run()方法
    @Scheduled(cron = "0 0 0 * * ? ")
    public void run(){
        SystemLog systemLog=new SystemLog();
        systemLog.setDate(LocalDateTime.now());
        systemLog.setVisits(0);
        systemLogService.save(systemLog);
        System.out.println("保存成功");
    }
}